package com.roc.admin.infrastructure.config;

import com.roc.admin.infrastructure.common.interceptor.AdminInterceptor;
import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * Web MVC配置类
 */
@Configuration
@AllArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {
    private static final Logger log = LoggerFactory.getLogger(WebMvcConfig.class);
    private final AdminInterceptor adminInterceptor;
    //    private final DataPermissionInterceptor dataPermissionInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册数据权限拦截器
        registry.addInterceptor(adminInterceptor)
                .addPathPatterns("/**")
                // 排除登录接口
                .excludePathPatterns("/user/login")
                // 排除Swagger相关接口
                .excludePathPatterns(Knife4jConfig.SWAGGER_WHITELIST);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // Knife4j与OpenAPI资源映射
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/");
        registry.addResourceHandler("/v3/api-docs/**").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/api-docs/**").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/swagger-resources/**").addResourceLocations("classpath:/META-INF/resources/swagger-resources/");
    }

    @PostConstruct
    public void postConstruct() {
        log.debug("[ROC ADMIN] - Auto Configuration 'Web MVC' completed initialization.");
    }
}
